package com.youzi.usercenter.service;

import com.youzi.corazon.common.exception.ServiceException;
import com.youzi.corazon.common.misc.IdGenerator;
import com.youzi.corazon.common.security.Md5Util;
import com.youzi.usercenter.form.RegisterForm;
import com.youzi.usercenter.model.TUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by tanxl on 2017-12-15.
 */
@Service
@Transactional
public class RegisterService {

    @Autowired
    UserService userService;

    public void register(RegisterForm form) {
        TUser user = userService.findByUsername(form.getUsername());
        if (user != null) {
            throw new ServiceException("用户名已存在");
        }

        user = new TUser();
        user.setId(IdGenerator.uuid2());
        user.setUsername(form.getUsername());
        user.setPassword(Md5Util.md5(form.getPassword()));
        user.setNickname(form.getUsername());
        user.setAvatar("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1270330953,3533676763&fm=27&gp=0.jpg");
        user.setStatus(1);

        userService.addUser(user);
    }
}
